/**
 * 
 */
package com.sx.article.biz.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSONArray;
import com.sx.article.biz.IAttachmentBiz;
import com.sx.article.dao.IArticleDao;
import com.sx.article.dao.IAttachmentDao;
import com.sx.article.entity.ArticleAndAttachmentEntity;
import com.sx.article.entity.AttachmentEntity;
import com.sx.article.entity.KeyWordAndArticleUnionEntity;
import com.sx.feesunit.exception.SXException;

/**
 * @author Administrator
 *
 */
public class AttachmentBiz implements IAttachmentBiz {
	private Log log = LogFactory.getLog(this.getClass());
	private IAttachmentDao attachmentdao;
	
	/* (non-Javadoc)
	 * @see com.sx.article.biz.IAttachmentBiz#deleteAttachment(com.sx.article.entity.AttachmentEntity)
	 */
	public boolean deleteAttachment(AttachmentEntity attachment)
			throws SXException {
		boolean flag = false;
		try {
			flag = attachmentdao.deleteAttachment(attachment);
		} catch (Exception e) {
			log.error("删除附件数据信息",e);
			throw new SXException();
		}
		return flag;
	}
	/* (non-Javadoc)
	 * @see com.sx.article.biz.IAttachmentBiz#findAttachmentInfoById(java.lang.Integer)
	 */
	public AttachmentEntity findAttachmentInfoById(Integer id)
			throws SXException {
		AttachmentEntity attachment = null;
		try {
			attachment = attachmentdao.findAttachmentInfoById(id);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("按照编号获取附件信息",e);
			throw new SXException();
		}
		return attachment;
	}
	/* (non-Javadoc)
	 * @see com.sx.article.biz.IAttachmentBiz#addAttachmentByArticleId(com.sx.article.entity.ArticleAndAttachmentEntity)
	 */
	public boolean addAttachmentByArticleId(
			ArticleAndAttachmentEntity articleattachment) throws SXException {
		boolean flag = false;
		try {
			flag = this.attachmentdao.addAttachmentByArticleId(articleattachment);
		} catch (Exception e) {
			log.error("新增附件数据信息",e);
			throw new SXException();
		}
		return flag;
	}
	/* (non-Javadoc)
	 * @see com.sx.article.biz.IAttachmentBiz#deleteAttachmentByArticleId(com.sx.article.entity.ArticleAndAttachmentEntity)
	 */
	public boolean deleteAttachmentByArticleId(
			ArticleAndAttachmentEntity articleattachment) throws SXException{
		boolean flag = false;
		try {
			flag = this.attachmentdao.deleteAttachmentByArticleId(articleattachment);
		} catch (Exception e) {
			log.error("删除附件数据信息",e);
			throw new SXException();
		}
		return flag;
	}
	/* (non-Javadoc)
	 * @see com.sx.article.biz.IAttachmentBiz#findAttachmentsInfo(com.sx.article.entity.AttachmentEntity)
	 */
	public List<AttachmentEntity> findAttachmentsInfo(AttachmentEntity attachemt)
			throws SXException {
		List<AttachmentEntity> list = null;
		try {
			list =  attachmentdao.findAttachmentsInfo(attachemt);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("附件数据信息",e);
			throw new SXException();
		}
		return list;
	}
	/* (non-Javadoc)
	 * @see com.sx.article.biz.IAttachmentBiz#findAttachmentsInfoCount(com.sx.article.entity.AttachmentEntity)
	 */
	public String findAttachmentsInfoCount(AttachmentEntity attachemt)
			throws SXException {
		String count = "";
		try {
			count = attachmentdao.findAttachmentsInfoCount(attachemt);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("附件数据信息数量信息",e);
			throw new SXException();
		}
		return count;
	}
	/* (non-Javadoc)
	 * @see com.sx.article.biz.IAttachmentBiz#findImageByUserInfoId(com.sx.article.entity.AttachmentEntity)
	 */
	public String findImageByUserInfoId(AttachmentEntity attachemt)
			throws SXException {
		StringBuffer buffer = new StringBuffer();
		try {
			List<AttachmentEntity> list =  findAttachmentsInfo(attachemt);
			String str = JSONArray.toJSONString(list);
			buffer.append("{images:"+str+"}");
		} catch (Exception e) {
			log.error("获取用户上传的图片数据信息",e);
			// TODO: handle exception
		}
		return buffer.toString();
	}
	/* (non-Javadoc)
	 * @see com.sx.article.biz.IAttachmentBiz#addAttachment(com.sx.article.entity.AttachmentEntity)
	 */
	public Integer addAttachment(AttachmentEntity attachment) throws SXException{
		Integer id = null;
		try {
			id = attachmentdao.addAttachment(attachment);
		} catch (Exception e) {
			log.error("新增文章数据信息",e);
			throw new SXException(e);
			// TODO: handle exception
		}
		return id;
	}
	/**
	 * @return the attachmentdao
	 */
	public IAttachmentDao getAttachmentdao() {
		return attachmentdao;
	}
	/**
	 * @param attachmentdao the attachmentdao to set
	 */
	public void setAttachmentdao(IAttachmentDao attachmentdao) {
		this.attachmentdao = attachmentdao;
	}
	
	
}
